<?php
class common extends CI_Controller {
	function classd() {
		$this -> pub -> logp('读取分类', '1-2-3');
		$da['infox'] = '';
		if ($this -> input -> post('pid') > 0) {
			$id = $this -> input -> post('table') . '_id';
			$this -> db -> select($id . ',name');
			$this -> db -> where('pid', $this -> input -> post('pid'));
			$this -> db -> where($this -> input -> post('table') . '_del', 1);
			$q = $this -> db -> get($this -> input -> post('table'));
			if ($q -> num_rows() > 0) {
				$da['infox'] = '<select named="' . $this -> input -> post('table') . '" class="classd form-control" style="width:150px;display: inline-block;" onchange="doclassd($(this))"><option idx="" value="">-请选择-</option>';
				foreach ($q->result_array() as $v) {
					$da['infox'] .= '<option idx="' . $v[$id] . '" value="' . $v['name'] . '">' . $v['name'] . '</option>';
				}
				$da['infox'] .= '</select>';
			}
		}
		echo json_encode($da);
	}

	//图片上传
	function kup() {
		$this -> pub -> logp('上传文本图片', '1-2-3');
		$cover = $this -> pub -> _up('imgFile');
		if (is_array($cover)) {
			$url = 'http://' . $this -> config -> item('oss_path') . '.oss.aliyuncs.com/' . $cover['url'] . $cover['file'];
			echo json_encode(array('error' => 0, 'url' => $url));
		} else {
			echo json_encode(array('error' => 1, 'message' => '抱歉，文件上传失败！'));
		}
	}

	function ukup() {
		$this -> pub -> logp('上传文本图片', '1-2-3');
		$cover = $this -> pub -> _up('upfile');
		if (is_array($cover)) {
			$url = 'http://' . $this -> config -> item('oss_path') . '.oss.aliyuncs.com/' . $cover['url'] . $cover['file'];
			$back = array("originalName" => $cover['name'], "name" => $cover['raw_name'], "url" => $url, "size" => $cover['size'], "type" => $cover['file_ext'], "state" => 'SUCCESS');
			echo json_encode($back);
		} else {
			echo json_encode(array('error' => 1, 'message' => '抱歉，文件上传失败！'));
		}
	}

	function up() {
		$this -> pub -> logp('上传单图片', '1-2-3');
		$error = "";
		$msg = "";
		$fileElementName = 'fileToUpload';
		if (!empty($_FILES[$fileElementName]['error'])) {
			switch($_FILES[$fileElementName]['error']) {

				case '1' :
					$error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
					break;
				case '2' :
					$error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
					break;
				case '3' :
					$error = 'The uploaded file was only partially uploaded';
					break;
				case '4' :
					$error = 'No file was uploaded.';
					break;

				case '6' :
					$error = 'Missing a temporary folder';
					break;
				case '7' :
					$error = 'Failed to write file to disk';
					break;
				case '8' :
					$error = 'File upload stopped by extension';
					break;
				case '999' :
				default :
					$error = 'No error code avaiable';
			}
		} elseif (empty($_FILES['fileToUpload']['tmp_name']) || $_FILES['fileToUpload']['tmp_name'] == 'none') {
			$error = 'No file was uploaded..';
		} else {
			$msg .= " File Name: " . $_FILES['fileToUpload']['name'] . ", ";
			$msg .= " File Size: " . @filesize($_FILES['fileToUpload']['tmp_name']);
			//for security reason, we force to remove all uploaded file
			@unlink($_FILES['fileToUpload']);
		}
		$data = $this -> pub -> _up('fileToUpload');
		echo "{";
		echo "error: '" . $error . "',\n";
		echo "value: '" . $data['url'] . $data['file'] . "',\n";
		echo "msg: '" . $this -> pub -> oss_dz($data['url'] . '_thumb' . $data['file']) . "',\n";
		echo "is_image: '" . $data['is_image'] . "',\n";
		echo "id: '" . $data['id'] . "'\n";
		echo "}";
	}

}
?>